//
// Created by wsc on 2023/8/15.
//
#include <iostream>
#include "main_widget_c.h"
#include "QtWidgets/QFileDialog"
#include "QtCore/QStringList"
#include "QtCore/QStringListModel"
#include "QtWidgets/QListWidgetItem"
#include "QtWidgets/QFileDialog"
#include "QtCore/QString"
#include "QtCore/QMap"
#include "convert_utils.h"
#include "QtCore/QThread"
#include "threads.h"
#include "QtWidgets/QMessageBox"
Window::Window() {

    ui.setupUi(this);
    this->init_data();
    this->init_widgets();

    this->init_slots();
    show();
}
Window::~Window() {

}


void Window::init_data() {
    list_model = new QStringListModel();
}

void Window::init_widgets() {
    ui.train_rate->setValue(0.8);
    ui.progress_bar->setValue(0);
    ui.dataset_list->setModel(list_model);
    ui.output->clear();
    this->setWindowTitle("YOLOv5数据集处理");
    this->setWindowIcon(QIcon(":icon/dataprocess.png"));
}

void Window::init_slots() {
    connect(ui.add_button, SIGNAL(clicked()), this, SLOT(add_dataset()));
    connect(ui.remove_button, SIGNAL(clicked()), this, SLOT(remove_dataset()));
    connect(ui.convert_button, SIGNAL(clicked()), this, SLOT(convert_dataset()));
    connect(this, SIGNAL(log_signal(QString)), this, SLOT(log(QString)));
    connect(this, SIGNAL(progress_signal(int)), this, SLOT(progress(int)));
}

bool check_dataset(const QString& dir){
    QDir qdir(dir);
    if (!qdir.cdUp()) {
        QMessageBox::warning(nullptr, "错误", "无法访问上一级目录");
        return false;
    }
    if (!qdir.exists("Annotations")) {
        QMessageBox::warning(nullptr, "错误", "Annotations文件夹不存在");
        return false;
    }
    return true;
}

// slots
void Window::add_dataset() {
// 选择文件夹
    QString dir_name = QFileDialog::getExistingDirectory(this, "选择文件夹", "");
    if (dir_name.isEmpty()) {
        return;
    }
    if (!check_dataset(dir_name)) {
        return;
    }
    this->list_model->insertRow(this->list_model->rowCount());
    QModelIndex index = this->list_model->index(this->list_model->rowCount()-1,0);
    this->list_model->setData(index, dir_name);
}

void Window::remove_dataset() {
    int current_index = ui.dataset_list->currentIndex().row();
    int rows = this->list_model->rowCount();
    if (current_index < 0 && rows > 0) {
        current_index = rows - 1;
    }
    // 删除选中的文件夹
    this->list_model->removeRow(current_index);
}

void Window::log(QString msg) {
    ui.output->append(msg);
}

void Window::progress(int value){
    ui.progress_bar->setValue(value);
}


void Window::convert_dataset() {
    QString output_dir = QFileDialog::getExistingDirectory(this, "选择文件夹", "");    
    if (output_dir.isEmpty()) {
        return;
    }
    // const QStringList& dataset_list = this->list_model->stringList();
    // QStringList dataset_list = {"D:\\deeplearning\\data\\glory2\\JPEGImages"};
    // QString output_dir = "C:\\Users\\wsc\\Desktop\\yolov5_dataset_process\\cpp\\test";
    delete this->convert_thread;
    this->convert_thread = new ConvertThread(this, output_dir);
    this->convert_thread->start();
    // ConvertUtils::convert(dataset_list, ui.train_rate->value(), output_dir, this);
}


